using System;
using System.Globalization;

namespace Oracle.ManagedDataAccess.Types
{
	internal static class DateTimeStringUtilities
	{
		private const char ZERO = '0';

		private const int DATE_TOKEN = 0;

		private const int TIME_TOKEN = 1;

		private const int REGION_TOKEN = 2;

		private const int TOKEN_COUNT = 3;

		private static char[] space = new char[1]
		{
			' '
		};

		private static char[] slash = new char[1]
		{
			'/'
		};

		private static char[] colon = new char[1]
		{
			':'
		};

		private static char[] dot = new char[1]
		{
			'.'
		};

		internal static string ToString(int year, int month, int day, int hours, int minutes, int seconds, int nanos = 0, string region = null)
		{
			string str = string.Empty;
			if (year < 0)
			{
				year *= -1;
				str = "-";
			}
			str += string.Format(CultureInfo.InvariantCulture, "{0}/{1}/{2} {3}:{4}:{5}", month.ToString("D2"), day.ToString("D2"), year.ToString("D4"), hours.ToString("D2"), minutes.ToString("D2"), seconds.ToString("D2"));
			if (nanos > 0)
			{
				str += ((double)nanos / 1000000000.0).ToString("F9", CultureInfo.InvariantCulture).Trim('0');
			}
			if (!string.IsNullOrWhiteSpace(region))
			{
				str = str + " " + region;
			}
			return str;
		}

		internal static void FromString(string stringRep, out int year, out int month, out int day, out int hours, out int minutes, out int seconds, out int nanos, out string region, bool expectNoRegion = false, bool expectNoNanos = false)
		{
			region = null;
			year = (month = (day = (hours = (minutes = (seconds = (nanos = 0))))));
			if (stringRep == null)
			{
				throw new ArgumentNullException();
			}
			string text = stringRep.Trim();
			if (text.Length == 0)
			{
				throw new FormatException();
			}
			bool flag = false;
			if (text[0] == '-')
			{
				flag = true;
			}
			if (flag || text[0] == '+')
			{
				text = text.Substring(1).TrimStart();
			}
			string[] array = text.Split(space, 3, StringSplitOptions.RemoveEmptyEntries);
			for (int i = 0; i < array.Length; i++)
			{
				string text2 = array[i].Trim();
				switch (i)
				{
				case 0:
				{
					string[] array2 = text2.Split(slash, StringSplitOptions.RemoveEmptyEntries);
					if (array2.Length != 3)
					{
						throw new FormatException();
					}
					month = int.Parse(array2[0]);
					if (month < 1 || month > 12)
					{
						throw new FormatException();
					}
					day = int.Parse(array2[1]);
					if (day < 1 || day > 31)
					{
						throw new FormatException();
					}
					year = int.Parse(array2[2]);
					if (flag)
					{
						year *= -1;
					}
					if (year < -4712 || year > 9999)
					{
						throw new FormatException();
					}
					break;
				}
				case 1:
				{
					string[] array3 = text2.Split(colon, StringSplitOptions.RemoveEmptyEntries);
					if (array3.Length != 3)
					{
						throw new FormatException();
					}
					hours = int.Parse(array3[0]);
					if (hours < 0 || hours > 23)
					{
						throw new FormatException();
					}
					minutes = int.Parse(array3[1]);
					if (minutes < 0 || minutes > 59)
					{
						throw new FormatException();
					}
					string[] array4 = array3[2].Split(dot, StringSplitOptions.RemoveEmptyEntries);
					seconds = int.Parse(array4[0]);
					if (seconds < 0 || seconds > 59)
					{
						throw new FormatException();
					}
					if (array4.Length == 2)
					{
						if (expectNoNanos)
						{
							throw new FormatException();
						}
						string s = "." + array4[1];
						nanos = (int)(1000000000.0 * double.Parse(s, CultureInfo.InvariantCulture));
						if (nanos < 0 || nanos > 999999999)
						{
							throw new FormatException();
						}
					}
					break;
				}
				case 2:
					if (!string.IsNullOrWhiteSpace(text2))
					{
						if (expectNoRegion)
						{
							throw new FormatException();
						}
						region = text2;
					}
					break;
				default:
					throw new FormatException();
				}
			}
		}
	}
}
